package com.foredu.fiqs.service.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import com.foredu.fiqs.domain.Clazz;
import com.foredu.fiqs.domain.Department;
import com.foredu.fiqs.domain.EmpPost;
import com.foredu.fiqs.domain.EmpTitle;
import com.foredu.fiqs.domain.EmpType;
import com.foredu.fiqs.domain.Employee;
import com.foredu.fiqs.domain.Role;
import com.foredu.fiqs.domain.SalaryItemDetailType;
import com.foredu.fiqs.domain.SalaryItemType;
import com.foredu.fiqs.domain.Student;
import com.foredu.fiqs.domain.Teacher;
import com.foredu.fiqs.domain.Term;
import com.foredu.fiqs.domain.User;
import com.foredu.fiqs.persist.IClazzDao;
import com.foredu.fiqs.persist.IDepartmentDao;
import com.foredu.fiqs.persist.IEmployeeDao;
import com.foredu.fiqs.persist.IEmppostDao;
import com.foredu.fiqs.persist.IEmptitleDao;
import com.foredu.fiqs.persist.IEmptypeDao;
import com.foredu.fiqs.persist.IRoleDao;
import com.foredu.fiqs.persist.ISalaryItemDetailTypeDao;
import com.foredu.fiqs.persist.ISalaryItemTypeDao;
import com.foredu.fiqs.persist.IStudentDao;
import com.foredu.fiqs.persist.ITeacherDao;
import com.foredu.fiqs.persist.ITermDao;
import com.foredu.fiqs.persist.IUserDao;
import com.foredu.fiqs.service.IManagementService;

@Transactional
public class ManagementService implements IManagementService {

	private IRoleDao roleDao = null;
	private IUserDao userDao = null;
	private IDepartmentDao deptDao = null;
	private IClazzDao clazzDao = null;
	private ITermDao termDao = null;
	private ITeacherDao teacherDao = null;
	private IStudentDao studentDao = null;
	private IEmptitleDao empTitleDao = null;
	private IEmppostDao empPostDao = null;
	private IEmptypeDao empTypeDao = null;
	private ISalaryItemTypeDao salaryItemTypeDao = null;
	private ISalaryItemDetailTypeDao salaryItemDetailTypeDao = null;
	private IEmployeeDao employeeDao=null;
	
	protected MessageSourceAccessor messages = SpringSecurityMessageSource
			.getAccessor();

	public ISalaryItemTypeDao getSalaryItemTypeDao() {
		return salaryItemTypeDao;
	}

	public void setSalaryItemTypeDao(ISalaryItemTypeDao salaryItemTypeDao) {
		this.salaryItemTypeDao = salaryItemTypeDao;
	}

	public void setSalaryItemDetailTypeDao(
			ISalaryItemDetailTypeDao salaryItemDetailTypeDao) {
		this.salaryItemDetailTypeDao = salaryItemDetailTypeDao;
	}

	public ISalaryItemDetailTypeDao getSalaryItemDetailTypeDao() {
		return salaryItemDetailTypeDao;
	}

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IDepartmentDao getDeptDao() {
		return deptDao;
	}

	public void setDeptDao(IDepartmentDao deptDao) {
		this.deptDao = deptDao;
	}

	public void setTeacherDao(ITeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public IClazzDao getClazzDao() {
		return clazzDao;
	}

	public ITeacherDao getTeacherDao() {
		return teacherDao;
	}

	public void setClazzDao(IClazzDao clazzDao) {
		this.clazzDao = clazzDao;
	}

	public ITermDao getTermDao() {
		return termDao;
	}

	public void setTermDao(ITermDao termDao) {
		this.termDao = termDao;
	}

	public IStudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(IStudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public IEmppostDao getEmpPostDao() {
		return empPostDao;
	}

	public void setEmpPostDao(IEmppostDao empPostDao) {
		this.empPostDao = empPostDao;
	}

	public IEmptitleDao getEmpTitleDao() {
		return empTitleDao;
	}

	public void setEmpTitleDao(IEmptitleDao empTitleDao) {
		this.empTitleDao = empTitleDao;
	}

	public MessageSourceAccessor getMessages() {
		return messages;
	}

	public void setMessages(MessageSourceAccessor messages) {
		this.messages = messages;
	}
	
	public void setEmployeeDao(IEmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	public IEmployeeDao getEmployeeDao() {
		return employeeDao;
	}
	
	
	public IEmptypeDao getEmpTypeDao() {
		return empTypeDao;
	}

	public void setEmpTypeDao(IEmptypeDao empTypeDao) {
		this.empTypeDao = empTypeDao;
	}

	@Override
	public void deleteRoleById(int id) {
		Role role = roleDao.findById(id);
		roleDao.delete(role);
	}

	@Override
	public void deleteUserById(int id) {
		User user = userDao.findById(id);
		userDao.delete(user);
	}

	@Override
	public Role findRoleById(int id) {
		return roleDao.findById(id);
	}

	@Override
	public List<Role> findRoles() {
		return roleDao.findAll();
	}

	@Override
	public User findUserById(int id) {
		return userDao.findById(id);
	}

	@Override
	public User findUserByUsername(String username) {
		return userDao.findUserByUserName(username);
	}

	@Override
	public List<User> findUsers() {
		return userDao.findAll();
	}

	@Override
	public List<User> findUsersByRoleName(String roleName) {
		return roleDao.findUsersByRoleName(roleName);
	}

	@Override
	public void saveRole(Role role) {
		roleDao.save(role);
	}

	@Override
	public void saveUser(User user) {
		userDao.save(user);
	}

	@Override
	public void updateRole(Role role) {
		roleDao.update(role);
	}

	@Override
	public void updateUser(User user) {
		userDao.update(user);
	}

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		User user = userDao.findUserByUserName(username);

		if (user == null) {
			throw new UsernameNotFoundException(messages.getMessage(
					"JdbcDaoImpl.notFound", new Object[] { username },
					"Username {0} not found"), username);
		}

		Set<Role> roles = user.getRoles();
		if (roles == null || roles.size() == 0) {
			throw new UsernameNotFoundException(messages.getMessage(
					"JdbcDaoImpl.notAuthority", new Object[] { username },
					"User {0} has no GrantedAuthority"), username);
		}

		Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
		for (Iterator iter = roles.iterator(); iter.hasNext();) {
			Role role = (Role) iter.next();
			GrantedAuthority authority = new GrantedAuthorityImpl("ROLE_"
					+ role.getName());
			authorities.add(authority);
		}

		return new org.springframework.security.core.userdetails.User(user
				.getUsername(), user.getPassword(), true, true, true, true,
				authorities);
	}

	@Override
	public void deleteDepartmentById(int id) {
		Department dept = deptDao.findById(id);
		deptDao.delete(dept);
	}

	@Override
	public Department findDepartmentById(int id) {
		return deptDao.findById(id);
	}

	@Override
	public Department findDepartmentByDeptId(String deptId) {
		return deptDao.findDepartmentByDeptId(deptId);
	}

	@Override
	public List<Department> findDepartments() {
		return deptDao.findAll();
	}

	@Override
	public void saveDepartment(Department department) {
		deptDao.save(department);
	}

	@Override
	public void updateDepartment(Department department) {
		deptDao.update(department);
	}

	@Override
	public void deleteClazzById(int id) {
		Clazz clazz = clazzDao.findById(id);
		clazzDao.delete(clazz);
	}

	@Override
	public Clazz findClazzById(int id) {
		return clazzDao.findById(id);
	}

	@Override
	public List<Clazz> findClazzs() {
		return clazzDao.findAll();
	}

	@Override
	public Integer saveClazz(Clazz clazz) {
		return clazzDao.save(clazz);
	}

	@Override
	public void updateClazz(Clazz clazz) {
		clazzDao.update(clazz);
	}

	@Override
	public void deleteTermById(int id) {
		Term term = termDao.findById(id);
		termDao.delete(term);
	}

	@Override
	public Term findTermById(int id) {
		return termDao.findById(id);
	}

	@Override
	public List<Term> findTerms() {
		return termDao.findAll();
	}

	@Override
	public void saveTerm(Term term) {
		termDao.save(term);
	}

	@Override
	public void updateTerm(Term term) {
		termDao.update(term);
	}

	@Override
	public void deleteTeacherById(int id) {

		User teacher = userDao.findById(id);
		userDao.delete(teacher);

	}

	@Override
	public Teacher findTeacherById(int id) {
		return teacherDao.findById(id);
	}

	@Override
	public Teacher findTeacherByTeachId(String teachId) {
		return teacherDao.findTeacherByTeachId(teachId);
	}

	@Override
	public Teacher findTeacherByUserId(int id) {
		return teacherDao.findById(id);
	}

	@Override
	public List<Teacher> findTeachers() {
		return teacherDao.findAll();
	}

	@Override
	public void saveTeacher(Teacher teacher) {
		teacherDao.save(teacher);
	}

	@Override
	public void updateTeacher(Teacher teacher) {
		teacherDao.update(teacher);
	}

	@Override
	public void deleteStudentById(int id) {
		User student = userDao.findById(id);
		userDao.delete(student);
	}

	@Override
	public Student findStudentById(int id) {
		return studentDao.findById(id);
	}

	@Override
	public List<Student> findStudents() {
		return studentDao.findAll();
	}

	@Override
	public void saveStudent(Student student) {
		studentDao.save(student);
	}

	@Override
	public void updateStudent(Student student) {
		studentDao.update(student);
	}

	public Student findStudentByStuId(String id) {
		return studentDao.findStudentByStuId(id);
	}

	@Override
	public Role findRoleByName(String name) {
		return roleDao.findRoleByName(name);
	}

	@Override
	public Clazz findClazzByClazzId(String clazzId) {
		return clazzDao.findClazzByClazzId(clazzId);
	}

	@Override
	public List<Teacher> findTeachersOfDept(Department dept) {
		return teacherDao.findTeacherOfDept(dept.getId());
	}

	@Override
	public Term findTermOfNow() {
		return termDao.findTermOfNow();
	}

	@Override
	public Term findTermByTimeAndOrder(int startYear, int endYear, int order) {
		return termDao.findTermByTimeAndOrder(startYear, endYear, order);
	}

	/**
	 * 职称管理
	 */

	@Override
	public void deleteEmpTitleById(int id) {

		EmpTitle em = this.empTitleDao.findById(id);
		this.empTitleDao.delete(em);
	}

	@Override
	public List<EmpTitle> findEmpTitle() {
		return this.empTitleDao.findAll();
	}

	@Override
	public EmpTitle findEmpTitleById(int id) {
		return this.empTitleDao.findById(id);
	}

	@Override
	public EmpTitle findEmpTitleByName(String name) {
		return null;
	}

	@Override
	public void saveEmpTitle(EmpTitle empTitle) {
		this.empTitleDao.save(empTitle);
	}

	@Override
	public void updateEmpTitle(EmpTitle empTitle) {
		this.empTitleDao.update(empTitle);

	}

	@Override
	public void deleteEmpPostById(int id) {
		EmpPost ep = this.empPostDao.findById(id);
		this.empPostDao.delete(ep);

	}

	@Override
	public List<EmpPost> findEmpPost() {
		return this.empPostDao.findAll();
	}

	@Override
	public EmpPost findEmpPostById(int id) {
		return this.empPostDao.findById(id);
	}

	@Override
	public void saveEmpPost(EmpPost empPost) {

		this.empPostDao.save(empPost);
	}

	@Override
	public void updateEmpPost(EmpPost empPost) {
		this.empPostDao.update(empPost);

	}

	/**
	 * 职务类型管理
	 */

	@Override
	public void deleteEmpTypeById(int id) {
		EmpType et = this.empTypeDao.findById(id);
		this.empTypeDao.delete(et);

	}

	@Override
	public EmpType findEmpTypeById(int id) {
		return this.empTypeDao.findById(id);
	}

	@Override
	public List<EmpType> findempType() {
		return this.empTypeDao.findAll();
	}

	@Override
	public void saveEmpType(EmpType empType) {
		this.empTypeDao.save(empType);

	}

	@Override
	public void updateEmpType(EmpType empType) {
		this.empTypeDao.update(empType);

	}

	@Override
	public void deleteSalaryItemType(int id) {
		SalaryItemType type = this.salaryItemTypeDao.findById(id);
		this.salaryItemTypeDao.delete(type);
	}

	@Override
	public List<SalaryItemType> findSalaryItemType() {
		return this.salaryItemTypeDao.findAll();
	}

	@Override
	public SalaryItemType findSalaryItemTypeById(int id) {
		return this.salaryItemTypeDao.findById(id);
	}

	@Override
	public void saveSalaryItemType(SalaryItemType type) {
		this.salaryItemTypeDao.save(type);
	}

	@Override
	public void updateSalaryItemType(SalaryItemType type) {
		this.salaryItemTypeDao.update(type);
	}

	@Override
	public void deleteSalaryItemDetailType(int id) {
		SalaryItemDetailType type = this.salaryItemDetailTypeDao.findById(id);
		this.salaryItemDetailTypeDao.delete(type);
	}

	@Override
	public List<SalaryItemDetailType> findDetailTypeOfSalaryItemType(
			SalaryItemType type) {
		return this.salaryItemDetailTypeDao.findDetailTypeOfSalaryItemType(type
				.getId());
	}

	@Override
	public List<SalaryItemDetailType> findSalaryItemDetailType() {
		return this.salaryItemDetailTypeDao.findAll();
	}

	@Override
	public SalaryItemDetailType findSalaryItemDetailTypeById(int id) {
		return this.salaryItemDetailTypeDao.findById(id);
	}

	@Override
	public void saveSalaryItemDetailType(SalaryItemDetailType type) {
		this.salaryItemDetailTypeDao.save(type);
	}

	@Override
	public void updateSalaryItemDetailType(SalaryItemDetailType type) {
		this.salaryItemDetailTypeDao.update(type);
	}

	@Override
	public void deleteEmployeeById(int id) {
		deleteUserById(id);
	}

	@Override
	public List<Employee> findEmployee() {
		return employeeDao.findAll();
	}

	@Override
	public Employee findempIdByEmpId(String empId) {
		return employeeDao.findByEmpId(empId);
	}

	@Override
	public void saveEmployee(Employee employee) {
		employeeDao.save(employee);
	}

	@Override
	public void updateEmployee(Employee employee) {
		employeeDao.update(employee);
	}

}
